package com.bfd;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.HashMap;
import java.util.Map;

/**
 * @author:yong.li
 * @date:2019/11/1 14:24
 */
public class ProducerSample {
    public static void main(String[] args){
        Map<String,Object> props = new HashMap<String, Object>();
        props.put("bootstrap.servers","localhost:9092");
        //序列化
        props.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer","org.apache.kafka.common.serialization.StringSerializer");
        //反序列化
        props.put("key.deserializer","org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer","org.apache.kafka.common.serialization.StringDeserializer");
        //配置zk
        props.put("zk.connect","127.0.0.1:2181");
        //topic
        String topic = "test-topic";
        //创建生产者对象
        Producer<String,String> producer = new KafkaProducer<String, String>(props);
        //发送消息
        //producer使用send（）就可以发送消息，入参类型为ProducerRecord（）
        //ProducerRecord(topic,partition,key,value)
        //ProducerRecord(topic,key,value)
        //ProducerRecord(topic,value)
        //partition和key是可选值。
        ProducerRecord<String,String> record = new ProducerRecord<String, String>("test-topic","message 1");

        producer.close();



    }
}
